package com.powernode.teatraceability.controller.contract;


import com.powernode.teatraceability.dao.login.UserMapper;
import com.powernode.teatraceability.pojo.basicinfomgt.Enterprise;
import com.powernode.teatraceability.pojo.login.User;
import com.powernode.teatraceability.service.contract.EnterpriseCertificateService;
import com.powernode.teatraceability.utils.GlobalConfig;
import com.powernode.teatraceability.utils.JsonData;
import com.powernode.teatraceability.vo.EnterpriseCertificateVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;

/**
 * ClassName: EnterpriseCertificateController
 * Description:
 *
 * @Author: jdz
 * @Create: 2023/4/27 - 14:06
 */
@RestController
@RequestMapping("/entcertificate")
public class EnterpriseCertificateController {
    @Resource
    private EnterpriseCertificateService service;
/*    @Resource
    private */
    @Value("${system.account.address}")
    private String accountAddress;
    @Resource
    private UserMapper userMapper;
    @PostMapping("/apply")
    public JsonData<String> applyEnterpriseCertificate(@RequestBody Enterprise inputBO) {
        /**
         * 存链上
         */
        final boolean result = service.applyEnterpriseCertificate(accountAddress,
                                                                  inputBO.getQymc(),
                                                                  inputBO.getFzr(),
                                                                  inputBO.getXydm(),
                                                                  inputBO.getBusinessLicense());
        if (result) {
            return JsonData.success("申请上链成功！");
        } else {
            return JsonData.failure(401, "申请上链失败！");
        }
    }
    @GetMapping("/info")
    public JsonData<EnterpriseCertificateVo> getEnterpriseCertificates(@SessionAttribute(name = "user", required = true) User user) {
        System.out.println(user);
        final EnterpriseCertificateVo enterpriseCertificates = service.getEnterpriseCertificate(accountAddress, BigInteger.valueOf(user.getQyIndex()));
        if (enterpriseCertificates != null) {
            enterpriseCertificates.setEvidence(user.getEvidence());
            return JsonData.success(enterpriseCertificates);
        } else {
            return JsonData.failure(401, null);
        }
    }
    @PutMapping("/update")
    public JsonData<String> modifyEnterpriseCertificate(@RequestBody Enterprise inputBO, @SessionAttribute(name = "user", required = true) User user) {
        final boolean result = service.changeEnterpriseCertificate( accountAddress,
                                                                    BigInteger.valueOf(user.getQyIndex()),
                                                                    inputBO.getQymc(),
                                                                    inputBO.getFzr(),
                                                                    inputBO.getXydm(),
                                                                    inputBO.getBusinessLicense());
        if (result) {
            return JsonData.success("修改上链信息成功！");
        } else {
            return JsonData.failure(401, "修改上链信息失败!");
        }
    }
    @GetMapping("/confirm")
    public JsonData<String> confirmEnterpriseCertificate(@SessionAttribute(name = "user", required = true) User user) {
        final String result = service.approveEnterpriseCertificate(accountAddress, BigInteger.valueOf(user.getQyIndex()));
        if (result != null) {
            user.setEvidence(result);
            final int i = userMapper.UpdateInfo(result, user.getRybh());
            if (i <= 0) {
                return JsonData.failure(401, "数据库更新失败！");
            }
            return JsonData.success("上链成功！");
        } else {
           return JsonData.failure(401, "false", "上链失败！");
        }
    }


}
